{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### This is a script to parse the output data from the SphygmoCor CVMS device for import into REDCap"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Import Modules"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "import pandas as pd\n",
    "import os\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Ask User for information"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Please enter the REDCap Record ID: 1\n",
      "Please enter the REDCap Event Name: sample8\n"
     ]
    }
   ],
   "source": [
    "record_id = int(input(\"Please enter the REDCap Record ID: \"))\n",
    "redcap_event_name = input('Please enter the REDCap Event Name: ')\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### List out file in directory to be parsed"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0] B_02Nov2015-084006_pwa.txt\n",
      "[1] B_03Nov2015-084352_pwa.txt\n",
      "[2] B_23Nov2015-075343_pwa.txt\n",
      "[3] B_23Nov2015-171440_pwa.txt\n",
      "[4] B_23Oct2015-145003_pwa.txt\n",
      "[5] B_25Nov2015-121409_pwa.txt\n",
      "[6] B_29Oct2015-104335_pwa.txt\n",
      "[7] B_30Oct2015-144315_pwa.txt\n",
      "[8] ProjectCITRUS_ImportTemplate_2015-11-25.csv\n",
      "Enter selection: 5\n"
     ]
    }
   ],
   "source": [
    "files = dict((str(i), f) for i, f in\n",
    "              enumerate(f for f in os.listdir(os.getcwd()) if f.endswith(('.txt','.csv'))))\n",
    "for item in sorted(files.items()):\n",
    "    print('[%s] %s' % item)\n",
    "choice = None\n",
    "while choice is None:\n",
    "    choice = files.get(input('Enter selection: '))\n",
    "    if not choice:\n",
    "        print('Please make a valid selection')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Import Data"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Define Import Parsing Function"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "def CVMS_Import(File):\n",
    "    \"\"\"This will parse the file and output a data frame of pressures waveforms\"\"\"\n",
    "    \n",
    "    # Read in file change column names and drop all na values\n",
    "    data = pd.read_csv(File, sep= '\\t', header=2)\n",
    "    data.columns = ['PeriphSignal', 'CentralSignal', 'PeriphPulse','CentralPulse','FlowWave', 'ForwardPluse','ReflectedPulse']\n",
    "    data = data.dropna()\n",
    "    \n",
    "    # Import \"Header Data\" and use to both save and add time column\n",
    "    data_head = pd.read_csv(File, sep='\\t', nrows=1)  \n",
    "    time, step = np.linspace(0, data_head['C_PERIOD'][0]/1000, \n",
    "                         num = len(data),endpoint=True, retstep=True)\n",
    "    #Add time to dataframe\n",
    "    data['Time'] = time\n",
    "    \n",
    "    return (data, data_head)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Define REDCap Headers"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "REDCAP_Headers = ['record_id', 'redcap_event_name', 'surname', 'first_name', 'sex',\n",
    "       'dob', 'patient_id', 'patient_code', 'patient_notes', 'sp', 'dp',\n",
    "       'mp', 'data_rev', 'datetime', 'age', 'medication', 'notes',\n",
    "       'operator', 'interpretation', 'height', 'weight', 'body_mass_index',\n",
    "       'sample_rate', 'simulation_mode', 'tonometer_serial_number',\n",
    "       'sub_type', 'inconclusive', 'reference_age', 'ppampratio',\n",
    "       'p_max_dpdt', 'ed', 'calced', 'quality_ed', 'p_qc_ph', 'p_qc_phv',\n",
    "       'p_qc_plv', 'p_qc_dv', 'p_qc_sdev', 'operator_index', 'p_sp',\n",
    "       'p_dp', 'p_meanp', 'p_t1', 'p_t2', 'p_ai', 'p_calct1', 'p_calct2',\n",
    "       'p_esp', 'p_p1', 'p_p2', 'p_t1ed', 'p_t2ed', 'p_quality_t1',\n",
    "       'p_quality_t2', 'c_ap', 'c_ap_hr75', 'c_mps', 'c_mpd', 'c_tti',\n",
    "       'c_dti', 'c_svi', 'c_al', 'c_ati', 'hr', 'c_period', 'c_dd',\n",
    "       'c_ed_period', 'c_dd_period', 'c_ph', 'c_agph', 'c_agph_hr75',\n",
    "       'c_p1_height', 'c_t1r', 'c_sp', 'c_dp', 'c_meanp', 'c_t1', 'c_t2',\n",
    "       'c_ai', 'c_calct1', 'c_calct2', 'c_esp', 'c_p1', 'c_p2', 'c_t1ed',\n",
    "       'c_t2ed', 'c_quality_t1', 'c_quality_t2', 'c_aprt', 'c_cftt',\n",
    "       'c_cfd', 'c_wrpwv', 'c_ri', 'c_fpph', 'c_fppt', 'c_rpph', 'c_rppt']"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Parse File"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "Output = CVMS_Import(choice)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Add Columns Needs to Input into REDCap"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "OutputForExport = Output[1].iloc[:,3:len(Output[1].columns)] # Extract columns for copying\n",
    "OutputForExport.insert(0,'record_id', record_id, allow_duplicates=False ) # Insert record_id value\n",
    "OutputForExport.insert(1,'redcap_event_name', redcap_event_name, allow_duplicates=False ) # Insert event name value\n",
    "OutputForExport['DATETIME'] = pd.to_datetime(OutputForExport.iloc[:,13]) # Format date and time\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Define columns to export for waveforms"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "WaveformsForExport = Output[0].iloc[:, 2:len(Output[0].columns)] # Define Waveforms for export"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Save the Demo/Derived Data to CSV File"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "filename, file_extension = os.path.splitext(choice)\n",
    "\n",
    "\n",
    "OutputForExport.to_csv('%s_BasicData.csv' %filename, header=REDCAP_Headers, index=False) # Export REDCAp data to file\n",
    "WaveformsForExport.to_csv('%s_Waveforms.csv' %filename, index=False) # Export waveforms to file"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.4.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
